package com.osmino.contacts.history;

import android.content.Context;
import android.database.Cursor;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import com.osmino.contacts.entity.Call;

public final class CallLogCursorAdapter extends CursorAdapter {

	private final SparseArray<Call> mCallInfoArray;
	
	public CallLogCursorAdapter(Context context, Cursor cursor) {
		super(context, cursor);
		mCallInfoArray = new SparseArray<Call>();
	}
	
	@Override
	public void changeCursor(Cursor cursor) {
		super.changeCursor(cursor);
		mCallInfoArray.clear();
	}
	
	@Override
	public Call getItem(int position) {
		return mCallInfoArray.get(position);
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		return new CallLogCell(context);
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		int position = cursor.getPosition();
		Call info = mCallInfoArray.get(position);
		if (info == null) {
			info = new Call(context, cursor);
			mCallInfoArray.put(position, info);
		}
		((CallLogCell) view).setCallInfo(info);
		((CallLogCell) view).setBackground((position % 2) != 0);
		
	}

}
